package thread_file;

import java.io.*;
import java.util.Random;
import java.util.concurrent.*;

/**
 * Description
 *
 * @ClassName thread_file
 * @Copyright 炫彩互动
 * @Author yanglh
 * @Create Date  2016/5/31
 * @Modified by none
 * @Modified Date
 */

public class SpiltFile {
    public static void main(String[] args) {
        //SpiltFile.spiltFile();
        SpiltFile.uninFile();
    }

    public static void spiltFile(){
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            File file = new File("E:\\书籍视频\\艾瑞\\20140314（网络)\\1安装CentOS.wmv");
            int fileSize = (int)file.length();
            fileInputStream = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            int len = 0;
            for(int i=1;i<= 5;i++){
                //   21/5 0-4  4-8 8-12    4
                fileOutputStream = new FileOutputStream("E:/"+i+".bak");
                len = fileInputStream.read(bytes);
                int countLen = len;
                while(len != -1 && (countLen <= fileSize/5)){
                    fileOutputStream.write(bytes,0,len);
                    len = fileInputStream.read(bytes);
                    countLen = countLen + len;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void uninFile(){
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            byte[] bytes = new byte[1024];
            int len = 0;
            fileOutputStream = new FileOutputStream("E:/all.wmv");
            for(int i=1;i<= 5;i++){
                //   21/5 0-4  4-8 8-12    4
                fileInputStream = new FileInputStream("E:/"+i+".bak");
                len = fileInputStream.read(bytes);
                while(len != -1){
                    fileOutputStream.write(bytes,0,len);
                    len = fileInputStream.read(bytes);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void spiltFileByThreadPool(String[] args){
        //使用JDK内置的线程池，创建具有5个内置线程的池
        Executor executor = Executors.newFixedThreadPool(5);
        for(int i=0; i<5; i++){
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getId()+":hello");
                }
            });   //每执行一次就在产生一个线程放入线程池
        }
    }
}
